#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
using namespace std;

struct data{
    string id;
    int score;
    int fnrk;
    int lcnb;
    int lcrk;
    data(string id,int score,int fnrk,int lcnb,int lcrk):id(id),score(score),fnrk(fnrk),lcnb(lcnb),lcrk(lcrk){}
};
int N,K;
bool cmp(data a,data b){
    if(a.score==b.score){return a.id<b.id;}
    return a.score>b.score;
}

int main(){
    //freopen("in.txt","r",stdin);
    int cnt=0;
    scanf("%d",&N);
    vector<data> glb_ranker;
    for(int i=1;i<=N;i++){
        scanf("%d",&K);
        cnt+=K;
        vector<data> lc_ranker;
        string id;int score;
        for(int j=0;j<K;j++){
            cin>>id>>score;
            lc_ranker.push_back(data(id,score,-1,i,-1));
        }
        sort(lc_ranker.begin(),lc_ranker.end(),cmp);
        int seq=0;
        for(int j=0;j<K;j++){
            if(j>0&&lc_ranker[j].score==lc_ranker[j-1].score){seq--;}
            else {seq=0;}
            lc_ranker[j].lcrk=seq+j+1;
            glb_ranker.push_back(lc_ranker[j]);
        }
    }
    printf("%d\n",cnt);
    sort(glb_ranker.begin(),glb_ranker.end(),cmp);
    int seq=0;
    for(int i=0;i<glb_ranker.size();i++){
        if(i>0 && glb_ranker[i].score==glb_ranker[i-1].score){seq--;}
        else{seq=0;}
        cout<<glb_ranker[i].id<<" "<<i+seq+1<<" "<<glb_ranker[i].lcnb<<" "<<glb_ranker[i].lcrk<<endl;
    }
}
